#include "zmq.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

int SendLogRequest(void* fd, std::string cmd, std::string fileName, std::string log)
{
    zmq_msg_t reply;

    zmq_msg_init_size(&reply, cmd.size());
    memcpy(zmq_msg_data(&reply), cmd.c_str(), cmd.size());
    zmq_msg_send(&reply, fd, ZMQ_SNDMORE);
    zmq_msg_close(&reply);

    zmq_msg_init_size(&reply, fileName.size());
    memcpy(zmq_msg_data(&reply), fileName.c_str(), fileName.size());
    zmq_msg_send(&reply, fd, ZMQ_SNDMORE);
    zmq_msg_close(&reply);

    zmq_msg_init_size(&reply, log.size());
    memcpy(zmq_msg_data(&reply), log.c_str(), log.size());
    zmq_msg_send(&reply, fd, 0);
    zmq_msg_close(&reply);

    return true;
}

int RecvLogMsg(void* fd, zmq_msg_t* cmd, zmq_msg_t* fileName, zmq_msg_t* log)
{
    zmq_msg_init(cmd);
    zmq_msg_init(fileName);
    zmq_msg_init(log);

    zmq_msg_recv(cmd, fd, 0);
    if (!zmq_msg_more(cmd)) {
        return false;
    }

    zmq_msg_recv(fileName, fd, 0);
    if (!zmq_msg_more(fileName)) {
        return false;
    }

    zmq_msg_recv(log, fd, 0);
    if (!zmq_msg_more(log)) {
        return true;
    }

    return false;
}
